package com.opgea.crm.repository;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TemporalType;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.common.util.DateUtil;
import com.opgea.crm.domain.entities.Activity;


@Repository
public class ActivityRepoImpl implements ActivityRepo{

	
	
	@PersistenceContext
	private EntityManager em;
	
	@Transactional
	public void create(Activity activity) {
		em.persist(activity);
	
	}

	public void remove(long id) {
		// TODO Auto-generated method stub
		Activity activity = em.find(Activity.class,id);
		em.remove(activity);
	}

	public Activity find(long id) {
		return em.find(Activity.class, id);
	}

	public List<Activity> findAll() {
		String query = "SELECT a FROM Activity a";
		List<Activity> activityList = em.createQuery(query, Activity.class).getResultList();
		return activityList;
	}

	public List<Activity> findAllByLeadId(long leadId) {
		String query = "SELECT a FROM Activity a WHERE a.lead.id = "+leadId;
		List<Activity> activityList = em.createQuery(query, Activity.class).getResultList();
		return activityList;
	}

	public List<Activity> findAllByActivityType(long leadId, int activityType) {
		String query = 	"SELECT a FROM Activity a WHERE a.lead.id = "+leadId
						+" AND a.activityType = "+activityType;
		List<Activity> activityList = em.createQuery(query, Activity.class).getResultList();
		return activityList;
	}
	
	@Override
	public List<Activity> findAllByDuration(Long assignedToId, Date from, Date to) {
		String queryString = "SELECT a from Activity a WHERE a.assignTo.id = :assignTo"
					  +" AND a.scheduledDate BETWEEN :fromDate AND :toDate ";
		Query query = em.createQuery(queryString, Activity.class);
		query.setParameter("assignTo", assignedToId);
		query.setParameter("fromDate", from, TemporalType.DATE);
		query.setParameter("toDate", to, TemporalType.DATE);
		List<Activity> activityList = query.getResultList();
		System.out.println("List Size : "+activityList.size());
		return activityList;
	}

	public void update(Activity activity) {
	    if (em.find(Activity.class, activity.getId()) == null) {
	        throw new IllegalArgumentException("Unknown employee id: " + activity.getId());
	      }
		Activity managedActivity = em.merge(activity);
	}

	@Override
	public List<Activity> findDynamically(String criteria, String value) {
		
		StringBuffer buffer=new StringBuffer("SELECT a FROM Activity a");
		String cond=" WHERE ";
		
		if(Integer.valueOf(criteria) == ApplicationConstant.ActivitySearchCriteriaQualifier.Opportunity.ordinal()){
			buffer.append(cond);
			buffer.append(" a.opportunity.opportunityTitle like :paramVal");
			}
		/*
		if(Integer.valueOf(criteria) == ActivitySearchCriteriaQualifier.Priority.ordinal()){
			buffer.append(cond);
			buffer.append(" a.priority like :paramVal");
		}*/
		
		if(Integer.valueOf(criteria)== ApplicationConstant.ActivitySearchCriteriaQualifier.AssignedTo.ordinal()){
			buffer.append(cond);
			buffer.append(" a.assignTo.firstName like :paramVal");
		}		
		
		Query query=em.createQuery(buffer.toString());
		query.setParameter("paramVal", value+'%');
		
		return (List<Activity>)query.getResultList();
	}	
	
	public List<Activity> findReport(String periodFrom, String periodTo,
									 String statusType, String priorityType, 
									 String activityType ){
		
		System.out.println(statusType+"-"+priorityType+"-"+activityType);
		
		StringBuffer buffer = new StringBuffer("SELECT a FROM Activity a");
		String cond = " WHERE ";
		if(statusType != null && (!statusType.equalsIgnoreCase("")) && (!statusType.equalsIgnoreCase("0"))){
			buffer.append(cond);
			buffer.append(" a.activityStatus.id = :status");
			cond = " AND ";
		}
		if(priorityType != null && (!priorityType.equalsIgnoreCase("")) && (!priorityType.equalsIgnoreCase("0"))){
			buffer.append(cond);
			buffer.append(" a.priority.id = :priority");
			cond = " AND ";
		}
		if(activityType != null && (!activityType.equalsIgnoreCase("")) && (!activityType.equalsIgnoreCase("0"))){
			buffer.append(cond);
			buffer.append(" a.activityType.id = :activity");
			cond = " AND ";
		}
		if( (periodFrom != null && (!periodFrom.equalsIgnoreCase(""))) &&
					(periodTo != null && (!periodTo.equalsIgnoreCase("")))){
			buffer.append(cond);
			buffer.append(" a.scheduledDate BETWEEN :fromDate AND :toDate");
			cond = " AND ";
		}		
		
		System.out.println("Created Query Start >> "+buffer.toString());

		Query query = em.createQuery(buffer.toString());
		if(statusType != null && (!statusType.equalsIgnoreCase("")) && (!statusType.equalsIgnoreCase("0"))){
			query.setParameter("status", new Long(statusType));
		}
		if(priorityType != null && (!priorityType.equalsIgnoreCase("")) && (!priorityType.equalsIgnoreCase("0"))){
			query.setParameter("priority", new Long(priorityType));
		}
		if(activityType != null && (!activityType.equalsIgnoreCase("")) && (!activityType.equalsIgnoreCase("0"))){
			query.setParameter("activity", new Long(activityType));
		}
		if( (periodFrom != null && (!periodFrom.equalsIgnoreCase(""))) &&
				(periodTo != null && (!periodTo.equalsIgnoreCase("")))){
			query.setParameter("fromDate", DateUtil.stringToDate(periodFrom), TemporalType.DATE);
			query.setParameter("toDate", DateUtil.stringToDate(periodTo), TemporalType.DATE);
		}
		
		List<Activity> result = query.getResultList();
		System.out.println("ActivityList : Report : " +result.size());
		return result;
	}


}
